package com.langshixiaobai.domain.service.discount.impl;

import com.langshixiaobai.domain.model.valobj.GroupBuyActivityDiscountVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service("MJ")
@Slf4j
public class MJCalculateService extends AbstractDiscountCalculateService{


    @Override
    protected BigDecimal doCalculate(BigDecimal originalPrice, GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount) {
        log.info("优惠策略折扣计算:{}", groupBuyDiscount.getDiscountType().getCode());
        // 折扣表达式 - 100,10 满100减10元

        String marketExpr = groupBuyDiscount.getMarketExpr();
        String[] split = marketExpr.split(",");
        BigDecimal x = new BigDecimal(split[0].trim());
        BigDecimal y = new BigDecimal(split[1].trim());

        // 不满足最低满减约束，则按照原价
        if (originalPrice.compareTo(x) < 0) {
            return originalPrice;
        }
        // 折扣价格
        BigDecimal deductionPrice = originalPrice.subtract(y);

        if (deductionPrice.compareTo(BigDecimal.ZERO) <= 0) {
            // 进行兜底
            return new BigDecimal(0.01);
        }

        return deductionPrice;
    }
}
